package com.muyu.danmo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoMountMessage;
import com.muyu.danmo.domain.DanmoPrintingMessage;
import com.muyu.danmo.service.IDanmoMountMessageService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 装裱信息Controller
 *
 * @author yangle
 * @date 2024-10-14
 */
@RestController
@Log4j2
@RequestMapping("/mountMessage")
public class DanmoMountMessageController extends BaseController
{
    @Autowired
    private IDanmoMountMessageService danmoMountMessageService;

    /**
     * 查询装裱信息列表
     */
//    @RequiresPermissions("system:message:list")
    @PostMapping("/list")
    public Result<PageInfo<DanmoMountMessage>> list(@RequestBody DanmoMountMessage danmoMountMessage)
    {
        PageHelper.startPage(danmoMountMessage.getPageNum(), danmoMountMessage.getPageSize());
        List<DanmoMountMessage> list = danmoMountMessageService.selectDanmoMountMessageList(danmoMountMessage);
        System.out.println(list);
        PageInfo<DanmoMountMessage> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }

    /**
     * 导出装裱信息列表
     */
//    @RequiresPermissions("system:message:export")
    @Log(title = "装裱信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoMountMessage danmoMountMessage)
    {
        List<DanmoMountMessage> list = danmoMountMessageService.selectDanmoMountMessageList(danmoMountMessage);
        ExcelUtil<DanmoMountMessage> util = new ExcelUtil<DanmoMountMessage>(DanmoMountMessage.class);
        util.exportExcel(response, list, "装裱信息数据");
    }

    /**
     * 获取装裱信息详细信息
     */
//    @RequiresPermissions("system:message:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {   LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
//        if (extracted()) return Result.notLoggedIn("请登录");
        log.error("获取信息");
        return Result.success(danmoMountMessageService.selectDanmoMountMessageById(id));
    }

//    private static boolean extracted() {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (loginUser == null){
//            return true;
//        }
//        return false;
//    }

    /**
     * 新增装裱信息
     */
//    @RequiresPermissions("system:message:add")
    @Log(title = "装裱信息", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoMountMessage danmoMountMessage)
    {

        return  danmoMountMessageService.insertDanmoMountMessage(danmoMountMessage);
    }

    /**
     * 修改装裱信息
     */
//    @RequiresPermissions("system:message:edit")
    @Log(title = "装裱信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoMountMessage danmoMountMessage)
    {
//        if (extracted()) return Result.notLoggedIn("请登录");
        return   danmoMountMessageService.updateDanmoMountMessage(danmoMountMessage);
    }

    /**
     * 删除装裱信息
     */
//    @RequiresPermissions("system:message:remove")
    @Log(title = "装裱信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return Result.success(danmoMountMessageService.deleteDanmoMountMessageByIds(ids));
    }


    /**
     * 查看自己详情
     */
    @GetMapping("/danmoMountMessageByUserId")
    public Result danmoMountMessageByUserId() {
        {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser==null){
                return Result.notLoggedIn("请登录");
            }
            Long userId = loginUser.getUserid();
//        Long userId = Long.valueOf(1); // 固定的用户ID
//            DanmoMountMessage list = danmoMountMessageService.danmoMountMessageByUserId(userId);
//            return danmoMountMessageService.danmoMountMessageByUserId(userId);
            DanmoMountMessage danmoMountMessage = new DanmoMountMessage();
            danmoMountMessage.setUserId(userId);
            List<DanmoMountMessage> list = danmoMountMessageService.selectDanmoMountMessageList(danmoMountMessage);
            return Result.success(list);
        }
    }

    /**
     * 查看装裱
     */
    @GetMapping("/danmoMountMessageUserId")
    public Result<List<DanmoMountMessage>> danmoMountMessageUserId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return error("请登录");
        }
        Long userId = loginUser.getUserid();
        DanmoMountMessage danmoMountMessage = new DanmoMountMessage();
        danmoMountMessage.setUserId(userId);
        List<DanmoMountMessage> list = danmoMountMessageService.selectDanmoMountMessageList(danmoMountMessage);
      return Result.success(list);
    }


}
